<?php

namespace App\Http\Controllers\BusinessData;

use App\Http\Conf\ResultInfo;
use App\Http\Controllers\Controller;
use App\Http\Models\Salesman;
use App\Http\Requests\Business\SalesmanRequest;
use App\Imports\SalesmanImport;
use ErrorException;
use Exception;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class SalesmanController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $models = Salesman::all();
        return view('business_data.salesman.index', ['models' => $models]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('business_data.salesman.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SalesmanRequest $request)
    {
        $model = new Salesman();
        $model->job_number = $request->job_number;
        $model->name = $request->name;
        if ($model->save()) {
            return redirect()->action('BusinessData\SalesmanController@show', $model);
        } else {
            dd("添加数据有异常！");
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = Salesman::find($id);
        return view('business_data.salesman.show', ['model' => $model]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $model = Salesman::find($id);
        return view('business_data.salesman.edit', ['model' => $model]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(SalesmanRequest $request, $id)
    {
        $model = Salesman::find($id);
        $model->job_number = $request->job_number;
        $model->name = $request->name;
        if ($model->save()) {
            return redirect()->action('BusinessData\SalesmanController@show', $model);
        } else {
            dd("添加数据有异常！");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = Salesman::find($id);
        if ($model->delete()) {
            return ['status' => ResultInfo::SUCCESS];
        } else {
            return ['status' => ResultInfo::UNKNOWN];
        }
    }


    public function import(Request $request)
    {
        if ($request->method() == 'GET') {
            return view('business_data.salesman.import');
        } elseif ($request->method() == 'POST') {
            if ($request->upload_file == null) {
                return redirect()->back()->with('fail', '请选择文件');
            }

            try {
                Excel::import(new SalesmanImport, request()->file('upload_file'));
            } catch (Exception $e) {
                return redirect()->back()->with('fail', $e->getMessage());
            }
            return redirect()->action('BusinessData\SalesmanController@index')->with('success', '导入数据成功！');
        }
    }
}
